/* Javascript functions used in the sparql search page */

var queryArea;
var queryText;
var userQuery;
var prefix;
var select;
var where_begin;
var where_end;
var rules;
var numThing;
var numTarget;
var sunject;
var predicate;
var object;
var subjSelected;
var predSelected;
var objSelected;
var limit;
var browserType;
var targetArray;
var orderTarget;
var orderIndex;
var subjLiteral;
var objLiteral;
var rstitle;
var tableSize;
var numSubmit;

function init() {
	subject = "";
	predicate = "";
	object = "";
	queryText = "";
	userQuery = "";
	rules = "";
	limit = "";
	targetArray = new Array();
	orderTarget = "";
	orderIndex = 0;
	numThing = 0;
	numTarget = 0;
	subjSelected = false;
	predSelected = false;
	objSelected = false;
	subjLiteral = "";
	objLiteral = "";
	rstitle = "";
	tableSize = "";
	numSubmit = 0;
	queryArea = document.getElementById('QueryArea');
	prefix = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + "\n" + 
				"PREFIX j.0: <http://cseg674.googlecode.com/svn/trunk/CSEG674/ontology/source.owl#> ";
	select = "SELECT DISTINCT ";
	where_begin = "WHERE {";
	where_end = "}";
}

function insertText(textArea, text) {
	textArea.value = text;
}

function setSubject(item) {
	var subjIndex = item.selectedIndex;
	subject = item[subjIndex].text;
}

function setPredicate(item) {
	var predIndex = item.selectedIndex;
	predicate = item[predIndex].text;
}

function setObject(item) {
	var objIndex = item.selectedIndex;	
	object = item[objIndex].text;
}

function genSparql() {
    var querySubj = ""; 
    var queryPred = "";
    var queryObj = ""; 
    var isObjNode = true;

	if (subject !== '') {   
    	querySubj = "      ?thing" + numThing + " rdf:type j.0:" + subject + " . " + "\n";
		
    	if (subjLiteral !== '') {
    		querySubj = querySubj + "      ?thing" + numThing + " j.0:named \"" + subjLiteral + "\" . " + "\n";
    	}
    	else {
    		var targetName = "?" + subject + "_" + numTarget;
			targetArray[numTarget] = subject;
    		querySubj = querySubj + "      ?thing" + numThing + " j.0:named " + targetName + " . " + "\n";
    		select = select + targetName + " ";
    		numTarget++;
    		rstitle = rstitle + targetName + "|";
    	}
	}    

	if (predicate !== '') {
		if (predicate === 'hasModifier') {
			var targetName2 = "?" + object + "_" + numTarget;
			targetArray[numTarget] = object;
			if (objLiteral !== '') {
				queryPred = "      ?thing" + numThing + " j.0:" + predicate + " \"" + objLiteral + "\" . " + "\n";
			}
			else {
				queryPred = "      ?thing" + numThing + " j.0:" + predicate + " " + targetName2 + " . " + "\n";
				select = select + targetName2 + " ";
			}
			numThing++;
			numTarget++;
			isObjNode = false;
			rstitle = rstitle + targetName2 + "|";
		}
		else if ((predicate === 'hasReturnType') || (predicate === 'hasType')) {
			var targetName2 = "?" + object + "_" + numTarget;
			targetArray[numTarget] = object;
			predicate = "hasType";
			if (objLiteral !== '') {
				queryPred = "      ?thing" + numThing + " j.0:" + predicate + " \"" + objLiteral + "\" . " + "\n";
			}
			else {
				queryPred = "      ?thing" + numThing + " j.0:" + predicate + " " + targetName2 + " . " + "\n";
				select = select + targetName2 + " ";
			}
			numThing++;
			numTarget++;
			isObjNode = false;
			rstitle = rstitle + targetName2 + "|";
		}
		else if (predicate === 'derivesFrom') {
			var targetName2 = "?" + object + "_" + numTarget;
			targetArray[numTarget] = object;
			if (objLiteral !== '') {
				queryPred = "      ?thing" + numThing + " j.0:" + predicate + " \"" + objLiteral + "\" . " + "\n      FILTER isLiteral(" + targetName2 + ") . \n";
			}
			else {
				queryPred = "      ?thing" + numThing + " j.0:" + predicate + " " + targetName2 + " . " + "\n      FILTER isLiteral(" + targetName2 + ") . \n";
			}
			queryPred = queryPred + "   }\n   UNION\n   {\n" + querySubj + "      ?thing" + numThing + " j.0:" + predicate + " ?thing" 
						+ (numThing + 1) + " . " + "\n";
		}
		else {
			queryPred = "      ?thing" + numThing + " j.0:" + predicate + " ?thing" + (numThing + 1) + " . " + "\n";
		}	
	}
    
	if (isObjNode && object !== '') {
    	queryObj = "      ?thing" + (numThing + 1) + " rdf:type j.0:" + object + " . " + "\n";

    	if (objLiteral !== '') {
			queryObj = queryObj + "      ?thing" + (numThing + 1) + " j.0:named \"" + objLiteral + "\" . " + "\n";
    	}
    	else {
    		var targetName2 = "?" + object + "_" + numTarget;
    		targetArray[numTarget] = object;
    		queryObj = queryObj + "      ?thing" + (numThing + 1) + " j.0:named " + targetName2 + " . " + "\n";
    		select = select + targetName2 + " ";
    		numTarget++;
    		rstitle = rstitle + targetName2 + "|";
    	}
    	numThing = numThing + 2;
	}    
    if (predicate === 'derivesFrom') {
		querySubj = "   {\n" + querySubj;
		queryObj = queryObj + "   }\n";
	}
    rules = rules + querySubj + queryPred + queryObj;
	queryText = prefix + "\n" + select + "\n" + where_begin + "\n" + rules + where_end;
	userQuery = userQuery + subject + "(" + subjLiteral + ")" + "-" + predicate + "-" + object + "(" + objLiteral + ")" + ";";
	insertText(queryArea, queryText);
}

function genSortList() {
	var sortList = "Sort by: <select id='orderBy' name='orderBy' style='width: auto; margin: 0;' onChange='setOrderTarget(this)'><option>None</option>";
	for (var i = 0; i < targetArray.length; i++) {
		sortList = sortList + "<option>" + targetArray[i] + "</option>";
	}
	sortList = sortList + "</select>";
	document.getElementById("sortList").innerHTML = sortList;
}

function checkLiteralSelect() {
	if (subjLiteral !== '' &&  objLiteral !== '') {
		alert("Please only choose name for one literal.");
	}
	else if (!subjSelected && predSelected && !objSelected) {
		alert("Missing Subject/Object!");
	} 
	else {
		document.SPARQLForm.limit.disabled = false;
		document.SPARQLForm.orderBy.disabled = false;
		document.SPARQLForm.sortMethod.disabled = false;
		document.SPARQLForm.TableSize.value = tableSize;
		document.SPARQLForm.NumSubmit.value = numSubmit;
		
		genSparql();
		genSortList();
		clearSelect('addRule');
	}	
}

function checkSubmit() {
	var query = document.SPARQLForm.QueryArea.value;
	document.SPARQLForm.UserQuery.value = userQuery; 
	document.SPARQLForm.ResultTitle.value = rstitle;
	
	if (query === '') {
		alert("Please form a query first!");
		return false;
	}
	else {
		return true;
	}
}

function clearQuery() {
	document.SPARQLForm.limit.disabled = true;
	document.SPARQLForm.orderBy.disabled = true;
	document.SPARQLForm.sortMethod.disabled = true;
	
	if (subject === '' && predicate === '' && object === '') {
		init();
	}
	else {
		queryText = "";
		rules = "";
		numThing = 0;
		numTarget = 0;
		select = "SELECT ";
	}
	userQuery = "";
	rstitle = "";
}

function setSubjSelected() {
	if (!subjSelected) {
		subjSelected = true;
	}
}

function setPredSelected() {
	if (!predSelected) {
		predSelected = true;
	}
}

function setObjSelected() {
	if (!objSelected) {
		objSelected = true;
	}
}

function checkSubj() {
	if (predSelected) {
		document.TRIPLEForm.PredOwl.disabled = true;
		predSelected = false;
	} 
	else if (objSelected) {
		document.TRIPLEForm.ObjOwl.disabled = true;
	}
}

function checkPred() {
	if (subjSelected) {
		document.TRIPLEForm.SubjOwl.disabled = true;
	}
	else if (objSelected) {
		document.TRIPLEForm.ObjOwl.disabled = true;
	}
}

function checkObj() {
	if (predSelected) {
		document.TRIPLEForm.PredOwl.disabled = true;
		predSelected = false;
	}
	else if (subjSelected) {
		document.TRIPLEForm.SubjOwl.disabled = true;
	}
}

function handleLiteral(item, id) {
	var literalIndex = item.selectedIndex;
	var literal = item[literalIndex].text;
	var defaultSubjSelect = "Choose a " + subject + "...";
	var defaultObjSelect = "Choose a " + object + "...";

	if (id === "SubjList") {
		if (literal !== defaultSubjSelect) {
			subjLiteral = literal;
		}
		else {
			subjLiteral = "";
		}
	}
	else if (id === "ObjList") {
		if (literal !== defaultObjSelect) {
			objLiteral = literal;
		}
		else {
			objLiteral = "";
		}
	}
}

function setLimit(item) {
	var limitText;
	var limitIndex = item.selectedIndex;
	limit = item[limitIndex].text;
	if (limit !== "" && limit !== "None") {
		if (orderTarget !== "" && orderTarget !== "None") {
			if (document.SPARQLForm.sortMethod.value === 'Descending') {
				limitText = queryText + "\n" + "ORDER BY DESC(?" + orderTarget + "_" + (orderIndex-1) + ")" + "\n" + "LIMIT " + limit;
			}
			else {
				limitText = queryText + "\n" + "ORDER BY ASC(?" + orderTarget + "_" + (orderIndex-1) + ")" + "\n" + "LIMIT " + limit;
			}
		}
		else {
			limitText = queryText + "\n" + "LIMIT " + limit;
		}
	}
	else {
		if (orderTarget !== "" && orderTarget !== "None") {
			if (document.SPARQLForm.sortMethod.value === 'Descending') {
				limitText = queryText + "\n" + "ORDER BY DESC(?" + orderTarget + "_" + (orderIndex-1) + ")";
			}
			else {
				limitText = queryText + "\n" + "ORDER BY ASC(?" + orderTarget + "_" + (orderIndex-1) + ")";
			}
		}
		else {
			limitText = queryText;
		}
	}
	insertText(queryArea, limitText);
}

function setOrderTarget(item) {
	var sortText;
	orderIndex = item.selectedIndex;
	orderTarget = item[orderIndex].text;
	
	if (orderTarget !== "" && orderTarget !== "None") {
		if (document.SPARQLForm.sortMethod.value === 'Descending') {
			if (limit !== "" && limit !== "None") {
				sortText = queryText + "\n" + "ORDER BY DESC(?" + orderTarget + "_" + (orderIndex-1) + ")" + "\n" + "LIMIT " + limit;
			}
			else {
				sortText = queryText + "\n" + "ORDER BY DESC(?" + orderTarget + "_" + (orderIndex-1) + ")";
			}
		}
		else {
			if (limit !== "" && limit !== "None") {
				sortText = queryText + "\n" + "ORDER BY ASC(?" + orderTarget + "_" + (orderIndex-1) + ")" + "\n" + "LIMIT " + limit;
			}
			else {
				sortText = queryText + "\n" + "ORDER BY ASC(?" + orderTarget + "_" + (orderIndex-1) + ")";
			}
		}
	}
	else {
		if (limit !== "" && limit !== "None") {
			sortText = queryText + "\n" + "LIMIT " + limit;
		}
		else {
			sortText = queryText;
		}
	}
	insertText(queryArea, sortText);
}

function setSortMethod(item) {
	var methodIndex = item.selectedIndex;
	var sortMethod = item[methodIndex].text;
	
	if (orderTarget !== "" && orderTarget !== "None") {
		if (sortMethod === 'Descending') {
			if (limit !== "" && limit !== "None") {
				sortText = queryText + "\n" + "ORDER BY DESC(?" + orderTarget + "_" + (orderIndex-1) + ")" + "\n" + "LIMIT " + limit;
			}
			else {
				sortText = queryText + "\n" + "ORDER BY DESC(?" + orderTarget + "_" + (orderIndex-1) + ")";
			}
		}
		else {
			if (limit !== "" && limit !== "None") {
				sortText = queryText + "\n" + "ORDER BY ASC(?" + orderTarget + "_" + (orderIndex-1) + ")" + "\n" + "LIMIT " + limit;
			}
			else {
				sortText = queryText + "\n" + "ORDER BY ASC(?" + orderTarget + "_" + (orderIndex-1) + ")";
			}
		}
		insertText(queryArea, sortText);
	}
}

function resetLiterals() {
	subjLiteral = "";
	objLiteral = "";
}

function setTable() {
	if (subjSelected && objSelected) {
		if (subjLiteral !== "" || objLiteral !== "") {
			tableSize = tableSize + "1";
		} else {
			tableSize = tableSize + "2";
		}
	} else {
		tableSize = tableSize + "1";
	}
	numSubmit++;
}